<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

// 引入模型
use App\Models\Business\Business as BusinessModel;
use App\Models\Product\Product as ProductModel;
use App\Models\Product\Cart as CartModel;

// 引入Validator 
use Illuminate\Support\Facades\Validator;


class Cart extends Controller
{
    public function add()
    {
        $proid = request('proid', 0);
        $busid = request('busid', 0);

        $businessinfo = BusinessModel::find($busid);

        if (!$businessinfo) {
            return error('用户不存在');
        }

        $productinfo = ProductModel::find($proid);

        if (!$productinfo) {
            return error('商品不存在');
        }

        $stock = bcsub($productinfo['stock'], 1);

        if ($stock < 0) {
            return error('库存不足');
        }

        $cartinfo = CartModel::where(['busid' => $busid,  'proid' => $proid])->first();

        if ($cartinfo) {
            // 数量
            $nums = bcadd($cartinfo['nums'], 1);
            // 商品总价
            $total = bcmul($productinfo['price'], $nums, 2);

            $result = CartModel::where('id', $cartinfo['id'])->update(['nums' => $nums, 'total' => $total]);

            if ($result === false) {
                return error('添加失败');
            } else {
                return success('添加成功');
            }
        } else {
            // 验证规则
            $rule = [
                'busid' => ['required'],
                'proid' => ['required'],
                'nums' => ['required'],
                'price' => ['required'],
                'total' => ['required']
            ];

            // 错误提示信息
            $message = [
                'busid.required' => '用户id必填',
                'proid.required' => '商品id必填',
                'nums.required' => '数量必填',
                'price.required' => '价格必填',
                'total.required' => '总价必填'
            ];


            // 商品总价
            $total = bcmul($productinfo['price'], 1, 2);

            $data = [
                'busid' => $busid,
                'proid' => $proid,
                'nums' => 1,
                'price' => $productinfo['price'],
                'total' => $total,
            ];

            // 校验器
            $validator = Validator::make($data, $rule, $message);
            if ($validator->fails()) {
                return error($validator->errors()->first());
            }
            $result = CartModel::create($data);

            if ($result === false) {
                return error('添加失败');
            } else {
                return success('添加成功');
            }
        }
    }
    // 列表
    public function index()
    {
        $busid = request('busid', 0);
        // 确认订单传过来的购物车ID
        $cartid = request('cartid', 0);
        $businessinfo = BusinessModel::find($busid);

        if (!$businessinfo) {
            return error('用户不存在');
        }
        $dp=CartModel::query();
        if($cartid!=0){
            $ids = explode(',', $cartid);
            // 批量查询ID值
            $dp->whereIn('id', $ids);
        }

        $data = $dp->with('product')->where('busid', $busid)->get();

        return success('', $data);
    }
    // 编辑
    public function edit()
    {
        $nums = request('nums', '');
        $busid = request('busid', 0);
        $cartid = request('cartid', 0);
        $businessinfo = BusinessModel::find($busid);
        if (!$businessinfo) {
            return error('用户不存在');
        }

        $cartinfo = CartModel::find($cartid);

        if (!$cartinfo) {
            return error('购物车商品不存在');
        }

        $productinfo = ProductModel::find($cartinfo['proid']);

        if (!$productinfo) {
            return error('商品不存在');
        }


        $stock = bcsub($productinfo['stock'], $nums);
        if ($stock < 0 && $cartinfo['nums'] < $nums) {
            return error('库存不足');
        }
        // 商品总价
        $total = bcmul($productinfo['price'], $nums, 2);

        $result = CartModel::where('id', $cartinfo['id'])->update(['nums' => $nums, 'total' => $total]);

        if ($result === false) {
            return error('操作失败');
        } else {
            return success('操作成功');
        }
    }

    // 删除
    public function del()
    {
        $busid = request('busid', 0);
        $cartid = request('cartid', 0);

        $businessinfo = BusinessModel::find($busid);

        if (!$businessinfo) {
            return error('用户不存在');
        }

        $cartinfo = CartModel::find($cartid);

        if (!$cartinfo) {
            return error('购物车商品不存在');
        }

        $result = CartModel::destroy($cartid);

        if ($result === false) {
            return error('删除失败');
        } else {
            return success('删除成功');
        }
    }
}
